/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.attachment;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JLabel;

import twaver.TWaverConst;
import twaver.network.ui.ComponentAttachment;
import twaver.network.ui.ElementUI;
import twaver.swing.RotatableLabel;

public class RightAttachment extends ComponentAttachment{
	
	private final static RotatableLabel label = new RotatableLabel(TWaverConst.LABEL_ORIENTATION_RIGHT){
		{
			this.setHorizontalAlignment(JLabel.CENTER);
		}
	};
	
	public RightAttachment(String name, ElementUI ui) {
		super(name, ui);
		this.updateLabel();
		this.setComponent(label);
	}

	private void updateLabel(){	
        label.setText(this.getLabelContent());
        label.setForeground(this.getLabelColor());
        
        this.setPosition(this.getLabelPosition());
        
        this.setBorderVisible(false);    
        this.setClosable(false);
        this.setBodyVisible(false);
        this.setShadowVisible(false);
        this.setStyle(TWaverConst.ATTACHMENT_STYLE_DEFAULT);
        this.setXOffset(0);
        this.setYOffset(0);
        
        Dimension size = label.getPreferredSize();
        this.setWidth(size.width + 4);
        this.setHeight(size.height + 4);
	}

	protected int getLabelPosition(){
		return TWaverConst.POSITION_RIGHT;
	}
	
	protected String getLabelContent(){
		return "123456";
	}
	
	protected Color getLabelColor(){
		return Color.BLUE;
	}
	
	protected void update() {
        this.updateLabel();
        this.invalidate();
    	super.update();
    }
	
	public void paint(Graphics2D g2d) {	
		String text = getLabelContent();
		if(text != null){
			super.paint(g2d);
		}
	}
	
	public Rectangle getBounds() {
		String text = getLabelContent();
		if(text == null){
			return null;
		}else{
			return super.getBounds();
		}
	}
}